import { createStore, Store, useStore as baseUseStore } from 'vuex'
import { setItem, getItem } from '@/utils/storage'
import { USER } from '@/utils/constants'
import moduleTab from './module/tab.js'

const state = {
  count: 0,
  isCollapse: false,
  user: getItem(USER),
}

export const key = Symbol('store')

// 创建一个新的 store 实例
export const store = createStore({
  modules: {
    tab: moduleTab
  },
  state,
  mutations: {
    increment(state) {
      state.count++
    },
    setIsCollapse(state, payload) {
      state.isCollapse = payload
    },
    setUser(state, payload) {
      const user = payload
      setItem(USER, user)
      state.user = user
    }
  }
})

// 定义自己的 `useStore` 组合式函数
export function useStore() {
  return baseUseStore(key)
}
